<?php


namespace App\Models;


use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Laravel\Passport\HasApiTokens;

class Customer extends Model
{
    use HasApiTokens;
    protected $fillable = [
        'name','remark', 'status'
    ];

    protected static function boot()
    {
        parent::boot();
        static::addGlobalScope('customer', function (Builder $builder) {
            if (is_customer()) {
                $builder->where('id', '=', request()->input('customer_id'));
            }
        });
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     * @author luffyzhao@vip.126.com
     */
    public function master(){
        return $this->hasOne(CustomerUser::class)->whereNull('role_id');
    }

    /**
     * @return bool
     * @author luffyzhao@vip.126.com
     */
    public function isNormal(){
        return $this->getAttribute('status') === '1';
    }
}
